import sys
sys.path.append("..")
import leancloud
import Cust.leancloudapi
GetApi = Cust.leancloudapi.GetApi()
ele = GetApi.getElection()
ei = GetApi.getElectioninfo()
class Election:
   def insertElection(self,title,classid,start,end,type,anonymous,vote,desc):
        classinfo = GetApi.getClass()
        cla = classinfo.create_without_data(classid)
        election = ele()
        election.set('ElectionTitle',title)
        election.set('ClassInfo',cla)
        election.set('StartTime',start)
        election.set('EndTime',end)
        election.set('Type',type)
        election.set('Anonymous',anonymous)
        election.set('VoteLimit',vote)
        election.set('ElectionDesc',desc)
        try:
            election.save()
            return 1110
        except Exception as e:
            print e.message
            return 1111
   def  insertElectionInfo(self,eleid,stu):
       el = ele.create_without_data(eleid)
       for foo in stu:
           eli = ei()
           eli.set('Election',el)
           st = GetApi.getStudent()
           stud = st.create_without_data(foo)
           eli.set('StudentInfo',stud)
           try:
               eli.save()
           except Exception as e:
                return 1211
       return 1210
   def delelection(self,id):
       el = ele.create_without_data(id)
       query = ei.query
       query.equal_to('Election',id)
       for foo in query.find():
           try:
               foo.destroy()
           except Exception as e:
               return 1221
       try:
           el.destroy()
           return 1220 
       except Exception as e:
           return 1221
   def querystu(self,classid):
       print classid
       cla = GetApi.getClass()
       clas = cla.create_without_data(classid)
       query = leancloud.Query('StudentInfo')      
       res = query.equal_to('ClassInfo',clas).find()
       stu_list = []
       for foo in res:
           id = foo.id
           no = foo.get('StudentNo')
           name = foo.get('StudentName')
           student = {'id':id,'no':no,'name':name}
           stu_list.append(student)
       return stu_list
   def queryElection(self):
       query = ele.query
       query.limit(1000)
       query.include('ClassInfo')
       res = query.find()
       ele_list = []
       for foo in res:
           id = foo.id
           limit = foo.get('VoteLimit')
           desc = foo.get('ElectionDesc')
           cla = foo.get('ClassInfo')
           classname = cla.get('ClassName')
           classid = cla.id
           start = foo.get('StartTime')
           end = foo.get('EndTime')
           title = foo.get('ElectionTitle')
           type = foo.get('Type')
           anonymous = foo.get('Anonymous')
           status = foo.get('Status')
           eleinfo = {'id':id,'limit':limit,'desc':desc,'class':classname,'start':start,'end':end,'title':title,'type':type,'anonymous':anonymous,'status':status,'classid':classid}
           ele_list.append(eleinfo)
       return ele_list
   

        
